<?php
class Deposit_model extends BZ_Model
{
	protected $_table = 'deposit';
	
	/**
	 * 统计总数
	 */
	public function total($params)
	{
		$this->db->select('deposit_id');
		$this->db->from($this->_table);
		$this->db->where('uid', $params['uid']);
		if (!empty($params['state'])) {
            $this->db->where('state', $params['state']);
        }
        if (!empty($params['deposit_id'])) {
            $this->db->where('deposit_id', $params['deposit_id']);
        }
        if (!empty($params['startData'])) {
            $this->db->where('created_at >=', $params['startData']. ' 00:00:00');
        }
        if (!empty($params['endData'])) {
            $this->db->where('created_at <=', $params['endData'].' 23:59:59');
        }
		return $this->db->count_all_results();
	}

	/**
	 * 查找充值记录
	 */
	public function page_list($params, $start, $count)
	{
		$this->db->select('deposit_id, amount, state, created_at, LEFT(`created_at`, 7) AS month', FALSE);
		$this->db->from($this->_table);
		$this->db->where('uid', $params['uid']);
		$this->db->limit($start, $count);
		if (!empty($params['state'])) {
            $this->db->where('state', $params['state']);
        }
        if (!empty($params['deposit_id'])) {
            $this->db->where('deposit_id', $params['deposit_id']);
        }
        if (!empty($params['startData'])) {
            $this->db->where('created_at >=', $params['startData']. ' 00:00:00');
        }
        if (!empty($params['endData'])) {
            $this->db->where('created_at <=', $params['endData'].' 23:59:59');
        }
		$this->db->order_by('deposit_id', 'DESC');
		return $this->db->get();
	}

	/**
	 * 查找一条充值信息
	 */
	public function findById($depositId, $uid)
	{
		$this->db->select('deposit_id, uid, bank_id, state, amount');
		$this->db->from($this->_table);
		$this->db->where('deposit_id', (int)$depositId);
		$this->db->where('uid', (int)$uid);
		$this->db->limit(1);
		return $this->db->get();
	}

}